//
//   RANetWorkUtil.m
//  RayoSmartBleKey
//
//  Created by Piccolo on 2017/8/6.
//  Copyright © 2017年 Piccolo. All rights reserved.
//

#import "RANetWorkUtil.h"

@implementation  RANetWorkUtil

+ (BOOL)netWorkIsOn{
    return RANetWorkServiceController.sharedManager.netWorkModel.netWorkOn;
}

+ (NSRANetWorkType)netWorkType {
    return RANetWorkServiceController.sharedManager.netWorkModel.netWorkType;
}

+ (NSString *)userAgent {
    NSString *userAgent = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
#if TARGET_OS_IOS
    // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
    userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
#elif TARGET_OS_WATCH
    // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
    userAgent = [NSString stringWithFormat:@"%@/%@ (%@; watchOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[WKInterfaceDevice currentDevice] model], [[WKInterfaceDevice currentDevice] systemVersion], [[WKInterfaceDevice currentDevice] screenScale]];
#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
    userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
#endif
    return userAgent;
}

+ (NSString *)singleTokenQueryString {
    NSDictionary *queryDic = [NSDictionary dictionaryWithObject:URL_TOKEN forKey:@"token"];
    NSString *queryStr = AFQueryStringFromParameters(queryDic);
    return queryStr;
}

+ (void)GET:(NSString *)URLString
                   parameters:(id)parameters
                     progress:(void (^)(NSProgress * _Nonnull))downloadProgress
                      success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                      failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager GET:URLString
      parameters:parameters
        progress:(void (^)(NSProgress * _Nonnull))downloadProgress
         success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
         failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure];
}

+ (void)POST:(NSString *)URLString
  parameters:(id)parameters
    progress:(void (^)(NSProgress * _Nonnull))uploadProgress
     success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
     failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager POST:(NSString *)URLString
       parameters:(id)parameters
         progress:(void (^)(NSProgress * _Nonnull))uploadProgress
          success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
          failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure];
}

+ (void)POST:(NSString *)URLString
  parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
    progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
     failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager POST:(NSString *)URLString
       parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
         progress:(void (^)(NSProgress * _Nonnull))uploadProgress
          success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
          failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure];
    
}

+ (void)PUT:(NSString *)URLString
 parameters:(id)parameters
    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
    failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager PUT:(NSString *)URLString
      parameters:(id)parameters
         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure];
}

+ (void)PUT:(NSString *)URLString
 parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
    success:(void (^)(NSURLResponse * _Nonnull response, id responseObject))success
    failure:(void (^)(NSURLResponse * _Nonnull response, NSError *error))failure
error:(NSError *__autoreleasing *)requestError {
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]
                                    multipartFormRequestWithMethod:@"PUT"
                                    URLString:(NSString *)URLString
                                    parameters:(NSDictionary *)parameters
                                    constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                                    error:(NSError *__autoreleasing *)requestError];
    AFURLSessionManager *manager = [AFURLSessionManager sharedManager];
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager uploadTaskWithStreamedRequest:(NSURLRequest *)request
                                               progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
                                      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                          if (error) {
                                              failure(response, error);
                                          } else {
                                              success(response, responseObject);
                                          }
                                      }];
    
    [uploadTask resume];
}


//[ RANetWorkUtil POST:URLString parameters:bodyDic progress:nil success:^(NSURLSessionTask *task, id responseObject) {
//
//} failure:^(NSURLSessionTask *task, NSError *error) {
//
//}];

//[ RANetWorkUtil PUT:URLString parameters:bodyDic success:^(NSURLSessionTask *task, id responseObject) {
//
//} failure:^(NSURLSessionTask *task, NSError *error) {
//
//}];

@end
